package com.base.cn.platform.os.controller.course.cou.user;

import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.service.course.cou.buy.CourseBuyRecordService;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 用户课程接口Controller
 *
 * @author s.li
 * @create 2018-09-07-10:10
 */
@RestController
@RequestMapping("/api/course")
public class ApiUserCourseController extends ApiBaseController {

    @Autowired
    private CourseBuyRecordService courseBuyRecordService;
    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 我的课程
     * @param userId 用户ID
     * @param currentPage 页码
     * @return Map<String,Object>
     */
    @RequestMapping("/myCourseList")
    public Map<String,Object> myCourseList(@RequestParam("userId") BigDecimal userId,
                                           @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        if(!DataUtil.idIsNotNull(userId)){
            return ResultUtil.ERROR("请登录后再操作");
        }
        Map<String,Object> params = new HashMap<>();
        params.put("userId",userId);
        params.put("courseStatus","SUCCESS");

        Map<String,Object> dataMap = courseBuyRecordService.findCourseBuyRecordFrontPage(params,currentPage,10,true,true,true,true,false,false,false);
        boolean gotMoneySwitch =false;
        TreeMap<String, String> sysConfigContext = sysConfigService.getSysConfigContext(ConfigName.ONOFF);
        if(StringUtils.isNotEmpty(sysConfigContext.get("iosWithdraw"))&&sysConfigContext.get("iosWithdraw").equals("ON")){
            gotMoneySwitch=true;
        }
        if(ObjectUtils.isNotEmpty(dataMap)&&ObjectUtils.isNotEmpty(dataMap.get("list"))){
            List<Map> list = (List<Map>) dataMap.get("list");
            for(Map data :list){
                data.put("iosWithdraw",gotMoneySwitch);
            }
        }

        return ResultUtil.SUCCESS(dataMap);
    }

    /**
     * 获取要激活的套餐下课程的数据
     * @param recordId 购买记录
     * @return Map<String,Object>
     */
    @RequestMapping("/getCourseBuyCourseBuyRecordById")
    public Map<String,Object> getActivationPackage(@RequestParam("userId") BigDecimal userId,
                                                   @RequestParam("recordId") BigDecimal recordId){
        return courseBuyRecordService.findPackageActivationById(recordId,userId);
    }

    /**
     * 执行激活套餐课程
     * @param recordId 购买记录ID
     * @param courseIds 选择的课程ID
     * @return  Map<String,Object>
     */
    @RequestMapping("/activationPackage")
    public Map<String,Object> activationPackage(@RequestParam("userId") BigDecimal userId,
                                                @RequestParam("recordId") BigDecimal recordId,
                                                @RequestParam("courseIds") String courseIds){
        return courseBuyRecordService.activationPackage(userId,recordId,courseIds);
    }
}
